#! lua
-- libraries

-- screen
local dim,sys,!,$ in riscos

local var = \ (x)
     local r0,r1,r2 = sys (0x35,-1,x) -- OS_ReadModeVariable
     => r2 end -- function

screen = {
size = \ ( ) -- screensize in pixels
       => var(11)<<var(4),var(12)<<var(5) end, -- function
         }
-- window

window = {
place = \ (w,b,size)
  ![b]=w;sys (0x400cb,0,b) -- Wimp_GetWindowState
  local ww,wh = ![b+12] - ![b+4],![b+16] - ![b+8]
  local sw,sh = size ( )
  ![b+4],![b+8] = (sw-ww)/2,(sh-wh)/2
  ![b+12],![b+16] = ![b+4] + ww,![b+8] + wh
  ![b+28] = -1;sys (0x400c5,0,b);end, -- function Wimp_OpenWindow
       }

-- icon


icon = {
set = \ (window,icon,s,valid,eor,clear,buf)
   ![buf]=window;![buf+4]=icon;sys (0x400ce,0,buf) -- Wimp_GetIconState
   ![buf+8]=eor;![buf+12]=clear;
   if s then
     $[![buf+28] ] = s
     $[![buf+32] ] = valid or ""
   end -- if
   sys (0x400cd,0,buf) end, -- function  Wimp_SetIconState
      }

-- task
local TASK = 0x4b534154

task = {
new = \ (title,mesgs,b,version,idle)
-- title is a buffer, idle is nil if wimp_poll else wimp_pollidle
-- handlers return non-nil value for exit from task
      local poll = idle and 0x400e1 or 0x400c7
      local init = \ (self)
              local r0,r1 = sys (0x400c0,version or 310,TASK,title,mesgs)
              self.handle = r1; end
      local respond = \ (self)
             local mesghandler in self
             local action = mesghandler[ ![b+16] ]
             if action then => action (self) end -- if
             end -- function
      local run = \ (self)
           local handler, preclosedown in self
           local reason,quit,action,err
           while not quit do
            reason,err = sys (poll,self.mask or 0,b,self.time)
            if reason then
              action = handler[reason]
              if action then
                quit,err = action (self)
                if err then self:report (err); err = nil end -- if
               end -- if
            else
             self:report (err); err = nil; quit = true
            end -- if
           end -- while
           if preclosedown then preclosedown (self) end -- if
           sys (0x400dd,self.handle) end
       local report = \ (self,s,flag)
             $[b] = "Lua_"..s.."\0"
             local r0,r1 = sys (0x400df,b,flag or 17,title)
             => r1 -- 0 no click, 1 OK, 2 Cancel
             end
       local ack = \ (self,react)
           local x,y,fname,ftype = ![b+28],![b+32],$[b+44],![b+40]
           ![b+12] = ![b+8];![b+16] = 4 -- DataLoadAck
           sys (0x400e7,19,b,0)
           if react then => react (x,y,fname,ftype) end -- if
           end
       local handler = { [17] = respond, [18] = respond, }
       local mesghandler = { [0] = \ (self) => true end, }
       => { b = b,
            init = init,
            run = run,
            report = report,
            ack = ack,
            handler = handler,
            mesghandler = mesghandler,
            title = title,
-- handle, mask, preclosedown, time, ....
          } end -- function
   }

